//
//  XTStatusRetweetView.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/20.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

class XTStatusRetweetView: UIView {

    
    var retweetStatus : XTStatus? {
        
        didSet {
            
            //内容
            if retweetStatus == nil {
                contentLabel.text = nil
            }else {
            
                let name = retweetStatus?.user?.screen_name ?? "未知用户"
                let text = retweetStatus?.text ?? ""
                
                contentLabel.text = "@\(name):\(text)"
            }
        
            //图片
            pictureView.pictureURLs = retweetStatus?.pictureURLs
            
            //更新约束
            //重新更新配图的顶部
            pictureView.snp_updateConstraints { (make) -> Void in
                var margin = cellMargin
                if retweetStatus == nil {
                    margin = 0
                }else if retweetStatus?.pictureURLs == nil || retweetStatus?.pictureURLs?.count == 0 {
                    margin = 0
                }
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
            
            //更新contenLabel更新约束
            contentLabel.snp_updateConstraints { (make) -> Void in
                var margin = cellMargin
                if retweetStatus == nil {
                    margin = 0
                }else if retweetStatus?.text == nil {
                    margin = 0
                }
                make.top.equalTo(self).offset(margin)
            }
            
            
            //更新self的约束
            self.snp_updateConstraints { (make) -> Void in
                var margin = cellMargin
                if retweetStatus == nil {
                    margin = 0
                }
                make.bottom.equalTo(pictureView).offset(margin)
                
            }
            
        }
    
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置子控件
        prepareUI()
        
    }
    
    func prepareUI() {
    
        //设置背景
//        backgroundColor = UIColor.lightGrayColor()
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        //设置约束
        
        //contentLabel
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(self).offset(cellMargin)
        }
        
        //pictureView
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.trailing.equalTo(self).offset(-cellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            //测试
//            make.height.equalTo(400)
        }
        
        //self
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView)
        }
        
    }
    
    
    
    //MARK: - 懒加载
    
    //内容
    private lazy var contentLabel : UILabel = {
        let label = UILabel()
        
        //设置
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * cellMargin
        label.text = "我是转发view的文本"
        
        return label
    }()
    
    
    //pictureView
    private lazy var pictureView : XTStatusPictrueView = XTStatusPictrueView()
    
    
}
